package com.jivesoftware.spark
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

import mx.core.Container;
import mx.core.IDataRenderer;
import mx.core.IFlexDisplayObject;
import mx.core.mx_internal;
import mx.skins.halo.AccordionHeaderSkin;
import mx.styles.CSSStyleDeclaration;
import mx.styles.ISimpleStyleClient;

use namespace mx_internal;

[AccessibilityClass(implementation = "mx.accessibility.AccordionHeaderAccImpl")]

public class CanvasButtonAccordionHeader extends CanvasButton implements IDataRenderer
{
	mx_internal static var createAccessibilityImplementation:Function;
	
	public function CanvasButtonAccordionHeader()
	{
		super();
		
		toggleChanged = false;
		mouseFocusEnabled = false;
		tabEnabled = false;
	}
	
	private var focusObj:DisplayObject;
	
	private var focusSkin:IFlexDisplayObject;
	
	private var _data:Object;
	
	override public function get data():Object
	{
		return _data;
	}
	
	override public function set data(value:Object):void
	{
		_data = value;
	}
	
	override public function set selected(value:Boolean):void
	{
		_selected = value;
		
		invalidateDisplayList();
	}
	
	private static function initializeStyles():void
	{
		var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration("CanvasButtonAccordionHeader");
		
		if (!selector)
		{
			selector = new CSSStyleDeclaration();
		}
		
		selector.defaultFactory = function():void
		{
			this.fontSize = 10;
			this.fontWeight = "bold";
			this.disabledSkin = mx.skins.halo.AccordionHeaderSkin;
			this.downSkin = mx.skins.halo.AccordionHeaderSkin;
			this.horizontalGap = 2;
			this.overSkin = mx.skins.halo.AccordionHeaderSkin;
			this.paddingLeft = 5;
			this.paddingRight = 5;
			this.selectedDisabledSkin = mx.skins.halo.AccordionHeaderSkin;
			this.selectedDownSkin = mx.skins.halo.AccordionHeaderSkin;
			this.selectedUpSkin = mx.skins.halo.AccordionHeaderSkin;
			this.skin = mx.skins.halo.AccordionHeaderSkin;
			this.textAlign = "left";
			this.upSkin = mx.skins.halo.AccordionHeaderSkin;
		}
		
		StyleManager.setStyleDeclaration("CanvasButtonAccordionHeader", selector, false);
	}
	
	initializeStyles();
	
	override protected function initializeAccessibility():void
	{
		if (CanvasButtonAccordionHeader.createAccessibilityImplementation != null)
		{
			CanvasButtonAccordionHeader.createAccessibilityImplementation(this);
		}
	}
	
	override protected function createChildren()
	{
		super.createChildren();
		
		var styleDecl:CSSStyleDeclaration = StyleManager.getStyleDeclaration(className);
		
		if (styleDecl)
		{
			var value:Number = styleDecl.getStyle("paddingLeft");
			if (!isNaN(value))
			{
				setStyle("paddingLeft", value);
			}
			value = styleDecl.getStyle("paddingRight");
			if (!isNaN(value))
			{
				setStyle("paddingRight", value);
			}
		}
	}
	
	override public function drawFocus(isFocused:Boolean):void
	{
		if (isFocused && !isEffectStarted)
		{
			if (!focusObj)
			{
				var focusClass:Class = getStyle("focusSkin");
				
				focusObj = new focusClass();
				
				var focusStyleable:ISimpleStyleClient = focusObj as ISimpleStyleClient;
				if (focusStyleable)
				{
					focusStyleable.styleName = this;
				}
				
				addChild(focusObj);
				
				focusSkin = focusObj as IFlexDisplayObject;
			}
			
			if (focusSkin)
			{
				focusSkin.move(0, 0);
				focusSkin.setActualSize(unscaledWidth, unscaledHeight);
			}
			focusObj.visible = true;
			
			dispatchEvent(new Event("focusDraw"));
		}
		else if (focusObj)
		{
			focusObj.visible = false;
		}
	}
	
	override mx_internal function layoutContents(unscaledWidth:Number, 
								unscaledHeight:Number, 
								offset:Boolean):void
	{
		super.layoutContents(unscaledWidth, unscaledHeight, offset);
		
		if (focusObj)
		{
			setChildIndex(focusObj, numChildren - 1);
		}
	}
	
	override protected function rollOverHandler(event:MouseEvent):void
	{
		super.rollOverHandler(event);
		
		var accordion:Container = Container(parent);
		if (accordion.enabled)
		{
			accordion.rawChildren.setChildIndex(this,
				accordion.rawChildren.numChildren - 1);
		}
	}
}
}
